<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>用户订单</title>
	<script type="text/javascript" src="js/jquery-3.5.1.min.js"></script>
	<script type="text/javascript" src="js/userJScript.js"></script>
    <link rel="stylesheet" type="text/css" href="layui/css/layui.css" />
	<link rel="stylesheet" href="./css/userStyle.css"/>
	<link rel="stylesheet" href="https://at.alicdn.com/t/font_2150071_oqmbug6i2f.css"/>
  </head>
  <style type="text/css">
	 body{
		 width:100%;
		 min-width: 700px;
		 height:100%
	 }
	 .searchBar{
		 width:100%;
		 margin-top: 15px;
		/* border: 2px solid #000000; */
	 }
	 .demoTable{
		 /* border: 1px solid #0000FF; */
		 height:80%;
		 width: 100%;
	 }
	 .container{
	   width: 350px;
	   height: 350px;
	   margin: 10px;
	   padding: 20px;
	   border-radius: 8px;
	   box-shadow: 0 3px 18px rgba(28, 19, 19, 0.5);
	   font-size: 16px;
	 }
	 .layui-form-item{
		 margin-left: 20px;
	 }
	 #saveOrUpdate{
		/* border: 1px solid #0000FF; */
		/* display: flex;
		align-items: center;
		justify-content: space-around; */
	 }

  </style>
  <body>
	  <!--添加或修改的弹出层开始 -->
	  <div style="display:none;" id="saveOrUpdate" >
		    <div class="container">
				<form class="layui-form" action="" method="post" id="adOrUpForm" lay-filter="adOrUpForm">
				<div class="layui-form-item" style="display: none; position: relative;margin-bottom: 30px;">
				  <div class="layui-inline"style="position: relative;left: -30px;" >
				    <label class="layui-form-label" style="width: 50px;">ID：</label>
				    <div class="layui-input-inline" >
				      <input style="width: 250px;" type="text" name="id" autocomplete="off" class="layui-input">
				    </div>
				  </div>
				</div>
		      <div class="layui-form-item" style="position: relative;left:-31px;margin-bottom: 30px;">
				  <label class="layui-form-label" style="width: 80px;">物品类型：</label>
		        <div class="layui-input-inline" style="position: relative; ">
		          <select name="type">
		            <option value="" selected="">选择垃圾类型</option>
		            <option value="1" >厨余垃圾</option>
		            <option value="2">有害垃圾</option>
		            <option value="3">可回收垃圾</option>
		            <option value="4">不可回收垃圾</option>
		          </select>
		        </div>
		      </div>

					<div class="layui-form-item" style="position: relative;margin-bottom: 30px;">
						<div class="layui-inline" style="position: relative;left: -30px;" >
							<label class="layui-form-label" style="width: 80px;">物品名称：</label>
							<div class="layui-input-inline" >
								<input style="width: 250px;" type="text" name="weight" lay-verify="number|required" autocomplete="off" class="layui-input" placeholder="请输入所添加物品名称">
							</div>
						</div>
					</div>
		  
		      <div class="layui-form-item" style="position: relative;margin-bottom: 30px;">
		        <div class="layui-inline" style="position: relative;left: -30px;" >
		          <label class="layui-form-label" style="width: 50px;">重量：</label>
		          <div class="layui-input-inline" >
		            <input style="width: 250px;" type="text" name="weight" lay-verify="number|required" autocomplete="off" class="layui-input" placeholder="请输入重量(以千克为单位)">
		          </div>
		        </div>
		      </div>
		  
		      <div class="layui-form-item" style="position: relative;margin-bottom: 50px;">
		          <label class="layui-form-label" style="width: 80px;position: relative;left: -30px;">预计收益：</label>
		        <div class="layui-inline"  style="width: 50px;position: relative;left: -30px;">
		          <input>
		        </div>
		      </div>
		      <div class="layui-form-item" style="width: 315px;">
		        <div class="layui-input-inline" style="float:right;">
		          <button class="layui-btn layui-icon layui-icon-username" lay-submit="" lay-filter="addOrUpGarb">发布</button>
		        </div>
		      </div>
			  </form>
			</div>
		  
	  </div>
	  <!--添加或修改的弹出层结束 -->
	  <!-- 搜索开始-->
	  <div class="searchBar">
		<form class="layui-form" action="">
		          <div class="layui-input-inline">
		            <select name="type">
		              <option value="" selected="">物品类别</option>
		              <option value="1" >厨余垃圾</option>
		              <option value="2">有害垃圾</option>
		              <option value="3">可回收垃圾</option>
					  <option value="4">不可回收垃圾</option>
		            </select>
		          </div>
		          <div class="layui-input-inline">
		            <select name="status">
						<option value="" selected="">状态</option>
						<option value="1" >全部订单</option>
						<option value="2">待接单</option>
						<option value="3">进行中</option>
						<option value="4">已完成</option>
						<option value="5">已结束</option>
					</select>
		            </select>
		          </div>
				  <div class="layui-input-inline" style="width: 100px;">
				    <input type="text" readonly="" class="layui-input" name="entryTime" id="entryTime" placeholder="入库时间">
				  </div>
				   <div class="layui-input-inline">
				        <button type="submit" class="layui-btn" lay-submit="" lay-filter="search">搜索</button>
				        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
				   </div> 
		</form>
	  </div>
	  <!--搜索结束 -->
	  <!-- 数据表格开始-->
	 <div class="demoTable">
		  <table class="layui-hide" id="user" lay-filter="user"></table> 
	</div>
	<!--数据表格结束  -->
   </body>
<!-- script开始 -->
	<!-- 头部工具栏 -->
	<script type="text/html" id="toolbarDemo">
		<div class="layui-btn-container">
			<a class="layui-btn layui-btn-sm" href="./garbageRecycleManage.html">查找</a>
			<a class="layui-btn layui-btn-sm" lay-event="add">发布</a>
			<a class="layui-btn layui-btn-sm" lay-event="deleteAll">批量删除</a>
		</div>
	</script>
	<!-- 头部工具栏结束 -->
	<!-- layui开始 -->
	<script src="layui/layui.js" charset="utf-8"></script>
	<script type="text/javascript">
	layui.use(['table','layer','form','laydate'], function(){
			var table = layui.table;
			var layer = layui.layer;
			var form = layui.form;
			var laydate=layui.laydate;
			var search;
	laydate.render({
			  elem: '#entryTime'
			});		
  //方法级渲染
  var tableIns=table.render({
    elem: '#user'
   ,url:'/garbage/list'
   ,method:'post'
   ,contentType: 'application/json'
   ,where:search
   ,even:true
   ,skin:'row'
	,height:'full-90'
	,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
    ,cols: [[
		{type: 'checkbox',fixed:'left',width: 90}
      ,{field:'id', title: '买方ID',width: 90}
      ,{field:'type', title: '物品类型',width: 150,align:'center',
	  templet:function(d){
	  		  switch(d.type){
	  			  case 1:
	  			  return "厨余垃圾";
	  			  case 2:
	  			  return "有害垃圾";
				  case 3:
				  return "可回收垃圾";
				  case 4:
				  return "不可回收垃圾";
	  		  }
	  }}
      ,{field:'weight', title: '名称',}
      ,{field:'status', title: '状态',
	  templet:function(d){
		  switch(d.status){
			  case 0:
			  return '<span style="color: #40AFFE;">在库</span>';
			  case 1:
			  return '<span style="color: #00FF00;">出库</span>';
		  }
	  }}
      ,{field:'origin', title: '单价',width:'10%',align:'center'}
	  ,{field:'createTime', title: '订单开始时间',width:'200',align:'center'}
	  ,{field:'createTime', title: '订单结束时间',width:'200',align:'center'}
	  ,{fixed:'right',title:'操作',width:"132",
			  templet:function (d) {
	  			if(d.transportId==null){
	  				return '<button class="layui-btn layui-btn-xs" lay-event="edit">编辑</button>\n' +
							'\t\t\t<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</button>'
				}else{
	  				return '<button class="layui-btn layui-btn-xs layui-btn-disabled" disabled="true" lay-event="edit">编辑</button>\n' +
							'\t\t\t<a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled"  disabled="true" lay-event="del">删除</button>'
				}
			  }
    }
    ]]
	,page: true
	  , parseData: function(res) { //res 即为原始返回的数据
		  return {
			  "code": res.code, //解析接口状态
			  "msg": res.msg, //解析提示文本
			  "count": res.data.total, //解析数据长度
			  "data": res.data.records //解析数据列表
		  };
	  }

	  , response: {
		  statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
	  }
	  ,request: {
    	pageName: "displayStart",
		  limitName: "displayLength"
	  }
	,done:function(res,curr,count){
		console.log(res);
	}
  });
  
	var url;//添加或修改的url;
	var mainIndex;//关闭的弹出层索引
  //打开添加页面
  function openAddUser(){
  	  mainIndex=layer.open({
  	    type: 1, 
  	    title:"添加需要回收的垃圾",
  	    content: $("#saveOrUpdate"),/* '传入任意的文本或html' *///这里content是一个普通的String
		area:['410px','460px'],
		anim:5,
		resize:false,
		scrollbar: false,
		success:function(index){
			$("#adOrUpForm")[0].reset();
			url="/garbage/saveOrUpdate";
		}
  	  });
  }
 
  //打开修改页面
  function openUpdateUser(data){
  	  mainIndex=layer.open({
  	    type: 1, 
  	    title:"修改需要回收的垃圾",
		area:['410px','460px'],
  	    content: $("#saveOrUpdate"),/* '传入任意的文本或html' *///这里content是一个普通的String
  	    anim:5,
  	    resize:false,
  	    scrollbar: false,
  		success:function(index){
  			form.val("adOrUpForm",data);
			url="/garbage/saveOrUpdate";
  		}
  	  });
  }
  /*添加或修改垃圾表单提交 */
  form.on('submit(addOrUpGarb)', function(data){
         //序列化表单数据
		  var params= $("#adOrUpForm").serialize();
		  layer.alert(params, {
            title: '最终的提交信息'
          })
          $.ajax({
            url:url,
            method:'POST',
            contentType: 'application/json',
            async:false,
            data:JSON.stringify(data.field),
            success: function(res) {
  				layer.alert(res.msg, {
  					end: function(){
  							if (res.code == 200) console.log("success");
            	         }
            	    })
					layer.close(mainIndex);
					tableIns.reload();
            	}
			
            })
            return false;
        });

/*搜索表单提交 */
  form.on('submit(search)', function(data){
		table.reload('user',{
			where: data.field,
			page:{
				curr :1,
			},
		})
	  return false;
        });

  //删除方法
  function del(codeId) {
	  layer.confirm("您确定要删除吗？",function(){
		  $.ajax({
			  type:"DELETE",
			  url: '/garbage/deleteByIds',
			  contentType:'application/json',
			  data:JSON.stringify({"ids":codeId}),
			  success:function (data) {
				  layer.closeAll('loading');
				  if(data.code==200){
					  layer.msg('删除成功！', {icon: 1,time:2000,shade:0.2});
					  location.reload(true);
				  }else{
					  layer.msg('删除失败！', {icon: 2,time:3000,shade:0.2});
				  }
			  }
		  })
	  })
  }

 //批量删除 Start
   function getCheckData(){
    var checkStatus = table.checkStatus('user');
    if(checkStatus.data.length==0){
     layer.msg('请先选择要删除的数据行！', {icon: 2});
     return;
    }
    var codeId = [];
    for(var i=0;i<checkStatus.data.length;i++){
    	if(checkStatus.data[i].status == 0){
			codeId.push(checkStatus.data[i].id);
		}else{
    		layer.msg("出库垃圾，不可删除");
		}
    }
   	del(codeId);
  };
//批量删除 End

 //头工具栏事件
  table.on('toolbar(user)', function(obj){
	var checkStatus=table.checkStatus(obj.config.id);
	var data=checkStatus.data;
    switch(obj.event){
      case 'add':
		openAddUser(); 
		break;
      case 'deleteAll':
		getCheckData();
		break;
      /* //自定义头工具栏右侧图标 - 提示
      case 'LAYTABLE_TIPS':
        layer.alert('这是工具栏右侧自定义的一个图标按钮');
      break; */
    };
  })

//监听行工具事件
  table.on('tool(user)', function(obj){
	var data = obj.data; //获得当前行数据
	var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
    if(layEvent === 'del'){
		del([data.id]);
    } else if(layEvent === 'edit'){
      openUpdateUser(data);
    }
  })

});
</script>
</html>